package com.np.toiectest;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.np.toiectest.dto.CauHoiDTO;
import com.np.toiectest.util.ConfigObject;

public class ReadingActivity extends Activity implements OnClickListener {

	private TextView number;
	private TextView tvQuestion;
	private TextView tvProcess;

	private RadioButton radionButtonA;
	private RadioButton radionButtonB;
	private RadioButton radionButtonC;
	private RadioButton radionButtonD;

	private Button btnNext;
	private Button btnCheck;
	private Button btnBack;

	private int currentNumber;
	private int startingNumber;
	private int totalQuestion;

	private ArrayList<CauHoiDTO> ds = new ArrayList<CauHoiDTO>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reading);

		number = (TextView) findViewById(R.id.textViewNumber);
		tvQuestion = (TextView) findViewById(R.id.tvQuestion);
		tvProcess = (TextView) findViewById(R.id.tvProcess);

		btnNext = (Button) findViewById(R.id.btnNext);
		btnCheck = (Button) findViewById(R.id.btnCheck);
		btnBack = (Button) findViewById(R.id.btnPrevious);

		btnCheck.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		btnNext.setOnClickListener(this);

		radionButtonA = (RadioButton) findViewById(R.id.radioReadingA);
		radionButtonB = (RadioButton) findViewById(R.id.radioReadingB);
		radionButtonC = (RadioButton) findViewById(R.id.radioReadingC);
		radionButtonD = (RadioButton) findViewById(R.id.radioReadingD);

		radionButtonA.setOnClickListener(this);
		radionButtonB.setOnClickListener(this);
		radionButtonC.setOnClickListener(this);
		radionButtonD.setOnClickListener(this);

		getData();

	}

	@SuppressWarnings("unchecked")
	private void getData() {
		Bundle bundle = getIntent().getExtras();
		ds = (ArrayList<CauHoiDTO>) bundle
				.getSerializable(ConfigActivity.QUESTIONs);
		if (ds.size() < 1) {
			return;
			// Some messages must be added here
		}

		ConfigObject object = (ConfigObject) bundle
				.getSerializable(ConfigActivity.CONFIGURATION);

		startingNumber = object.getCauHoiHienTai();
		currentNumber = 0;
		totalQuestion = object.getSoLuongCauHoi();

		loadCauHoi();

	}

	private void loadCauHoi() {
		try {
			CauHoiDTO cauHoi = ds.get(currentNumber);
			radionButtonA.setText(cauHoi.getDapAnA());
			radionButtonB.setText(cauHoi.getDapAnB());
			radionButtonC.setText(cauHoi.getDapAnC());
			radionButtonD.setText(cauHoi.getDapAnD());

			tvQuestion.setText(cauHoi.getTenCauHoi());

			// set label process
			tvProcess.setText((startingNumber + currentNumber + 1) + " / " + totalQuestion);

		} catch (Exception e) {
			e.toString();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void finish(int resultCode) {
		Intent intent = getIntent();

		int score = 200;
		intent.putExtra("score", score);
		intent.putExtra("currentNumber", currentNumber + startingNumber + 0);
		setResult(resultCode, intent);

		finish();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnNext:
			if (currentNumber + 1 < ds.size()) {
				currentNumber++;

				loadCauHoi();
			} else {
				finish(ConfigActivity.REQUEST_CODE_ID_READING_PART);
			}
			break;
		case R.id.btnPrevious:
			if (currentNumber - 1 >= 0) {
				currentNumber--;
				loadCauHoi();
			}
			break;
		case R.id.btnCheck:
			if (ds.get(currentNumber).getDapAnChon() == ds.get(currentNumber)
					.getDapAnDung()) {
				Toast.makeText(this, "It's correct!", Toast.LENGTH_SHORT)
						.show();
			} else {
				Toast.makeText(this, "It's incorrect!", Toast.LENGTH_LONG)
						.show();
			}
			break;
		case R.id.radioReadingB:
			ds.get(currentNumber).setDapAnChon(1);
			break;
		case R.id.radioButtonB:
			ds.get(currentNumber).setDapAnChon(2);
			break;
		case R.id.radioButtonC:
			ds.get(currentNumber).setDapAnChon(3);
			break;
		case R.id.radioButtonD:
			ds.get(currentNumber).setDapAnChon(4);
			break;
		default:
			break;
		}

	}

}
